﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhanMemDoVui.BusinessLogic;
using PhanMemDoVui.BusinessObject;

namespace PhanMemDoVui
{
    public partial class frmCheckQuestion : Form
    {
        private QuestnAns question = new QuestnAns();

        public frmCheckQuestion()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Lamp;
        }

        private void frmCheckQuestion_Load(object sender, EventArgs e)
        {
            AdminBUS admin = AdminBUS.Instance();
            grid_check.DataSource = admin.DuyetCauHoi();
            btn_add.Enabled = false;
            btn_remove.Enabled = false;
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            frmAdmin admin = new frmAdmin();
            this.Hide();
            admin.Show();
        }

        private void grid_check_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            btn_add.Enabled = true;
            btn_remove.Enabled = true;
            //dong hien tai
            int dong = e.RowIndex;
            question.Question = grid_check.Rows[dong].Cells["Question"].Value.ToString();
            //question.Ans1 = grid_check.Rows[dong].Cells["Ans1"].Value.ToString();
            //question.Ans2 = grid_check.Rows[dong].Cells["Ans2"].Value.ToString();
            //question.Ans3 = grid_check.Rows[dong].Cells["Ans3"].Value.ToString();
            //question.Ans4 = grid_check.Rows[dong].Cells["Ans4"].Value.ToString();
            //question.RAns = grid_check.Rows[dong].Cells["R_Ans"].Value.ToString();
            //question.Difficulty = grid_check.Rows[dong].Cells["Difficulty"].Value.ToString();
            //question.CreatedBy = grid_check.Rows[dong].Cells["Created_by"].Value.ToString();
        }

        private void btn_remove_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có chắc muốn xóa vĩnh viễn câu hỏi này?", "Xóa câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                QuestionsBUS qt = new QuestionsBUS();
                AdminBUS admin = AdminBUS.Instance();
                if (qt.XoaCauHoi(question))
                    MessageBox.Show("Đã xóa câu hỏi", "Xóa câu hỏi");
                else
                    MessageBox.Show("Lỗi khi xóa", "Xóa câu hỏi");
                grid_check.DataSource = admin.DuyetCauHoi();
            }
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            QuestionsBUS qt = new QuestionsBUS();
            AdminBUS admin = AdminBUS.Instance();
            if (qt.ChuyenCauHoi(question))
                MessageBox.Show("Thêm thành công", "Thêm câu hỏi");
            else
                MessageBox.Show("Lỗi khi thêm", "Thêm câu hỏi");
            grid_check.DataSource = admin.DuyetCauHoi();
        }

        private void frmCheckQuestion_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
